﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using enqueteJwVd;
using enqueteJwVd.Models;
using enqueteJwVd.DAL;
using enqueteJwVd.Services;
using enqueteJwVd.ViewModels;


namespace enqueteJwVd.Controllers
{
    [Authorize(Roles = "e-manager")]
    public class QuestionGroupsController : Controller
    {
        UnitOfWork unitOfWork = new UnitOfWork();
        IQuestionGroupService questionGroupService;
       // private VDLDbEntities db = new VDLDbEntities();

        //
        // GET: /QuestionGroups/

        public QuestionGroupsController(IQuestionGroupService questionGroupService)
        {
            this.questionGroupService = questionGroupService;
        }

        public QuestionGroupsController()
        {
            
        }
        
        public ViewResult Index()
        {
            var questionGroups = questionGroupService.GetAllQuestionGroups();
            var viewModel = new QuestionGroupIndexViewModel() { QuestionGroups = questionGroups };
            //var questiongroup = db.QuestionGroup.Include(q => q.Survey);
            return View(viewModel);
        }

        //
        // GET: /QuestionGroups/Details/5

        public ViewResult Details(int? id)
        {
            QuestionGroup questiongroup = unitOfWork.QuestionGroupRepository.GetById(id);
            return View(questiongroup);
        }

        //
        // GET: /QuestionGroups/Create

        public ActionResult Create()
        {
           /* overbodig vanwege viewbag
            * var surveyIds = from s in unitOfWork.SurveyRepository.Get()
                         select s;
            * */
            ViewBag.RolId = new SelectList(unitOfWork.RoleRepository.Get(),"Id", "RolName");
            ViewBag.SurveyFkId = new SelectList( unitOfWork.SurveyRepository.Get(), "Id", "Title");
            return View();
        } 

        //
        // POST: /QuestionGroups/Create

        [HttpPost]
        public ActionResult Create([Bind(Exclude = "Id")]QuestionGroup questiongroup)
        {
            if (ModelState.IsValid)
            {
                unitOfWork.QuestionGroupRepository.
                    Add(questiongroup);
                unitOfWork.SaveChanges();
                return RedirectToAction("Index");  
            }

            
            return View(questiongroup);
        }
        
        //
        // GET: /QuestionGroups/Edit/5
 
        public ActionResult Edit(int? id)
        {
            QuestionGroup questiongroup = unitOfWork.QuestionGroupRepository.GetById(id);
           
            return View(questiongroup);
        }

        //
        // POST: /QuestionGroups/Edit/5

        [HttpPost]
        public ActionResult Edit(QuestionGroup questiongroup)
        {
            if (ModelState.IsValid)
            {
                unitOfWork.QuestionGroupRepository.Update(questiongroup);
                unitOfWork.SaveChanges();
                return RedirectToAction("Index");
            }
           
            return View(questiongroup);
        }

        //
        // GET: /QuestionGroups/Delete/5
 
        public ActionResult Delete(int? id)
        {
            QuestionGroup questiongroup = unitOfWork.QuestionGroupRepository.GetById(id);
            return View(questiongroup);
        }

        //
        // POST: /QuestionGroups/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int? id)
        {            
            QuestionGroup questiongroup = unitOfWork.QuestionGroupRepository.GetById(id);
            unitOfWork.QuestionGroupRepository.Delete(questiongroup);
            unitOfWork.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            unitOfWork.Dispose();
            base.Dispose(disposing);
        }
    }
}